 jQuery(function() {
	 jQuery("#source").val(jQuery("#source1").val());
	 jQuery("#sourceType").val(jQuery("#sourceType1").val());
	 jQuery("#sourcePower").val(jQuery("#sourcePower1").val());

	 jQuery("#chancePriority").val(jQuery("#chancePriority1").val());
	 if(jQuery("#id").val().length==0){
		 document.getElementById("sourceType").options[0].selected=true;
	 }
///订货单位片区
	 jQuery("#orderUnitArea").select2().trigger("change");
	 jQuery("#sourceArea").select2().trigger("change");
//引入客户信息弹窗	 
	 jQuery('#queryCustomer').on("click",function(){
		 jQuery("#queryCustomerModal").modal('show');
	 });
//引入客户信息	 
	 jQuery('#pull-in').on("click",function(){
		 var selects=getSelect("#customerTable",5);
	   	   if(selects.length>1){
	   		   $.springLemonDialog({
	   		         content: '只能操作一条记录',// 确认文本
	   		         clickButton: function (sender, modal, index) {// 按钮事件处理
	   		             $(this).closeDialog(modal);
	   		         }
	   		   }); 
	   	   }
	   	   else if(selects.length==0){
	   		   $.springLemonDialog({
	   		         content: '请选择要操作的记录',// 确认文本
	   		         clickButton: function (sender, modal, index) {// 按钮事件处理
	   		             $(this).closeDialog(modal);
	   		         }
	   		   }); 
	   	   }else if(selects.length==1){
	   		jQuery("#customerId").val(selects[0].customerId);
	   		jQuery("#orderUnit").val(selects[0].customerName);
	   		jQuery("#customerId").val(selects[0].id);
	   		jQuery("#orderUnitAddr").text(selects[0].address);
	   		jQuery("#busine").val(selects[0].business).selected = true;
	   		jQuery("#orderUnitArea").select2().val(selects[0].saleArea.superiorArea).trigger("change");
	   		$(".checkCustomer").prop("checked", false);
	   		jQuery("#queryCustomerModal").modal('hide');
	   	   }
	 });
//客户关键人物添加
	jQuery('#addBtn').on("click",function()
	{
		jQuery("#keyManName").val("");
		jQuery("#business").val("");
		jQuery("#phone").val("");
		jQuery("#callName").val("");
		jQuery("#keyMemo").val("");
		var chanceId = jQuery("#id").val();
		if(chanceId == ""){
		$.springLemonDialog({
  		         content: '即将保存该销售信息，是否继续？',// 确认文本
  		         clickButton: function (sender, modal, index) {// 按钮事件处理
  		             $(this).closeDialog(modal);
  		   		if(index == 0){
  					if (jQuery("#chanceRegisterForm") && !jQuery("#chanceRegisterForm").Validform({
  						tiptype : 3,
  						showAllError : true
  					}).check(false)) {return;}
  					var api = window.rootPath + "/ui/chance/save";
  					var d={
  							"isSubmit" : $("#isSubmit").val(),
  							"creator" : $("#creator").val(),
  							"user":{"id":$("#userId").val()},
  							"sourceSuperior" : $("#sourceSuperior").val(),
  							"name" : $("#name").val(),
  							"source" : $("#source").val(),
  							"chancePriority" : $("#chancePriority").val(),
  							"sourceArea" : $("#sourceArea").val(),
  							"sourceType" : $("#sourceType").val(),
  							"sourceUnit" : $("#sourceUnit").val(),
  							"orderAmount" : $("#orderAmount").val(),
  							"projectState" : $("#projectState").val(),
  							"deviceState" : $("#deviceState").val(),
  							"sourcePower" : $("#sourcePower").val(),
  							"orderUnit" : $("#orderUnit").val(),
  							"customerId" : $("#customerId").val(),
  							"orderPerson" : $("#orderPerson").val(),
  							"orderPersonPhone" : $("#orderPersonPhone").val(),
  							"orderUnitArea" : $("#orderUnitArea").val(),
  							"orderUnitAddr" : $("#orderUnitAddr").val(),
  							"sourceOfferInfo" : $("#sourceOfferInfo").val(),
  							"memo" : $("#memo").val()
  					};
  					jQuery.ajax({
  						type : 'post',
  						url : api,
  						data : JSON.stringify(d),
  						dataType : 'json',
  						contentType : 'application/json;charset=utf-8',
  						success : function(data) {
  							jQuery(".saveBtn").removeAttr("disabled");
  							var chanceId = data.chanceId;
  							jQuery("#id").val(chanceId);
  						}
  					});

  				jQuery("#kehuModal").modal('show');

  				}
  		         }
  		 });

		}else{
			jQuery("#kehuModal").modal('show');
		}
	});
	//客户关键人物编辑
	jQuery('.editBtn').on("click",function()
	{
		var selects=getSelect("#keyManDataTable",1);
	   	   if(selects.length>1){
	   		   $.springLemonDialog({
	   		         content: '只能操作一条记录',// 确认文本
	   		         clickButton: function (sender, modal, index) {// 按钮事件处理
	   		             $(this).closeDialog(modal);
	   		         }
	   		   }); 
	   	   }
	   	   else if(selects.length==0){
	   		   $.springLemonDialog({
	   		         content: '请选择要操作的记录',// 确认文本
	   		         clickButton: function (sender, modal, index) {// 按钮事件处理
	   		             $(this).closeDialog(modal);
	   		         }
	   		   }); 
	   	   }else if(selects.length==1){
	   		 var id=selects[0].id;
	   		jQuery("#keyManId").val(id);
	   		jQuery("#kehuModal").modal('show');
	   		jQuery("#keyManName").val(selects[0].keyManName);
			jQuery("#business").val(selects[0].business);
			jQuery("#phone").val(selects[0].phone);
			jQuery("#callName").val(selects[0].callName);
			jQuery("#keyMemo").val(selects[0].keyMemo);
	   	   }
	});
	//信息保存按钮
	jQuery('.saveBtn').on("click",function(){
	if (jQuery("#chanceRegisterForm") && !jQuery("#chanceRegisterForm").Validform({
		tiptype : 3,
		showAllError : true
	}).check(false)) {return;}
	jQuery(".saveBtn").attr("disabled","disabled");
	var id = $("#id").val();
	if(id == ""){
	var api = window.rootPath + "/ui/chance/save";
	var d={
			"customerId" : $("#customerId").val(),
			"isSubmit" : $("#isSubmit").val(),
			"creator" : $("#creator").val(),
			"user":{"id":$("#userId").val()},
			"sourceSuperior" : $("#sourceSuperior").val(),
			"name" : $("#name").val(),
			"source" : $("#source").val(),
			"business" : $("#busine").val(),
			"chancePriority" : $("#chancePriority").val(),
			"sourceArea" : $("#sourceArea").val(),
			"sourceType" : $("#sourceType").val(),
			"sourceUnit" : $("#sourceUnit").val(),
			"orderAmount" : $("#orderAmount").val(),
			"projectState" : $("#projectState").val(),
			"deviceState" : $("#deviceState").val(),
			"sourcePower" : $("#sourcePower").val(),
			"orderUnit" : $("#orderUnit").val(),
			"customerId" : $("#customerId").val(),
			"orderPerson" : $("#orderPerson").val(),
			"orderPersonPhone" : $("#orderPersonPhone").val(),
			"orderUnitArea" : $("#orderUnitArea").val(),
			"orderUnitAddr" : $("#orderUnitAddr").val(),
			"sourceOfferInfo" : $("#sourceOfferInfo").val(),
			"memo" : $("#memo").val()
	};
	jQuery.ajax({
		type : 'post',
		url : api,
		data : JSON.stringify(d),
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			jQuery(".saveBtn").removeAttr("disabled");
			var chanceId = data.chanceId;
			jQuery("#id").val(chanceId);
			window.location = window.rootPath + "/ui/chance";
		}
	});
	}
	else{
		var url = window.rootPath + "/api/chances/"+id;
		var d={
				"name" : $("#name").val(),
				"source" : $("#source").val(),
				"chancePriority" : $("#chancePriority").val(),
				"sourceArea" : $("#sourceArea").val(),
				"sourceType" : $("#sourceType").val(),
				"sourceUnit" : $("#sourceUnit").val(),
				"orderAmount" : $("#orderAmount").val(),
				"projectState" : $("#projectState").val(),
				"deviceState" : $("#deviceState").val(),
				"sourcePower" : $("#sourcePower").val(),
				"orderUnit" : $("#orderUnit").val(),
				"customerId" : $("#customerId").val(),
				"orderPerson" : $("#orderPerson").val(),
				"orderPersonPhone" : $("#orderPersonPhone").val(),
				"orderUnitArea" : $("#orderUnitArea").val(),
				"orderUnitAddr" : $("#orderUnitAddr").val(),
				"sourceOfferInfo" : $("#sourceOfferInfo").val(),
				"memo" : $("#memo").val(),
				"business" : $("#busine").val()
		};
		jQuery.ajax({
			type : 'patch',
			url : encodeURI(url),
			data : JSON.stringify(d),
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				    var operateType = "信息登记";
				    var content = "编辑销售信息【"+$("#name").val()+"】";
				    addOperateLog(operateType,content);
					jQuery(".saveBtn").removeAttr("disabled");
					window.location = window.rootPath + "/ui/chance";
			}
		});
	}
	});
	
	//信息关键人物保存
	jQuery('#KeyManSave').on("click",function(){
	if (jQuery("#keyManForm") && !jQuery("#keyManForm").Validform({
		tiptype : 3,
		showAllError : true
	}).check(false)) {return;}
	jQuery("#KeyManSave").attr("disabled","disabled");
	var url = window.rootPath + "/ui/chanceKeyMan/save";
	var keyManId = jQuery("#keyManId").val();
	if(keyManId == ""){
		var d={
				"keyManName" : $("#keyManName").val(),
				"business" : $("#business").val(),
				"phone" : $("#phone").val(),
				"callName" : $("#callName").val(),
				"keyMemo" : $("#keyMemo").val(),
				"saleChance":{"id":$("#id").val()}
		};
	}else{
		 d={
				"id" : keyManId,
				"keyManName" : $("#keyManName").val(),
				"business" : $("#business").val(),
				"phone" : $("#phone").val(),
				"callName" : $("#callName").val(),
				"keyMemo" : $("#keyMemo").val(),
		};
		
	}
		jQuery.ajax({
			type : 'post',
			url : url,
			data : JSON.stringify(d),
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				serverData.chanceId =$("#id").val();
	    		serverTable.draw();
	    		jQuery("#KeyManSave").removeAttr("disabled");
				jQuery('#kehuModal').modal('hide');
				
			}
		});
	

	});
	//信息关键人物datatable
    var serverData = {};
    var serverTable;
    jQuery(document).ready(function () {
    	serverData.chanceId =$("#id").val();
        serverTable = jQuery('#keyManDataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/chanceKeyMan/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            "iDisplayLength": 5,
            columns: [
                {
                data: null
                },
                {
                	data:null,
        		    render: function(data, type, full, meta){
        		    	var operate ='<input type="checkbox" class="checkchild">';
        		    	var id=data.id;
        		    	return operate;
        						
        				}
                },
                {
                    data: 'keyManName'
                },
                {
                    data: 'business'
                },
                {
                    data: 'phone'
                },
                {
                    data: 'callName'
                    
                },
                {
                    data: 'keyMemo',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                    
                } 
              ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

                serverTable.off('click', 'tr');
                serverTable.on('click', 'tr', function (event) {
                    event = event || window.event;
                    var target = event.target || event.scrElement;
                    var data = serverTable.row(this).data();

                    return;
                });
            }
        });
    	});
    var serverData1 = {};
    serverData1.key="";
    var serverTable1;
    jQuery(document).ready(function () {
        serverTable1 = jQuery('#customerTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/customer/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData1);
                }
            },
            "iDisplayLength": 5,
            columns: [
                {
                data: null
                },
                {
                	data: 'customerName'
                },
                {
                    data: 'saleArea.areaName'
                },
                {
                    data: 'business'
                },
                {
                    data: 'superior'
                },
                {
                	data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var operate =//'<button type="button" class="btn btn-sm btn-info editBtn" title="编辑"><i class="fa fa-pencil"></i></button>'
                                       '<input type="checkbox" class="checkCustomer">';
                        return operate;

                    }
                    
                } 
              ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

                serverTable1.off('click', 'tr');
                serverTable1.on('click', 'tr', function (event) {
                    event = event || window.event;
                    var target = event.target || event.scrElement;
                    var data = serverTable1.row(this).data();

                    return;
                });
            }
        });
    	});
    //搜索
	jQuery('.searchBtn').on('click', function() {
		serverData1.key = jQuery('#keyword').val();
		serverTable1.draw();
	})
	// 撤销查询
	jQuery('.resetBtn').on("click", function() {
		jQuery('#keyword').val('');
		serverData1.key = '';
		serverTable1.draw();
	});
	//删除关键人物
	jQuery('#deleteBtn').on("click",function(){
		var selects=getSelect("#keyManDataTable",1);
    	var url = window.rootPath + "/ui/chanceKeyMan/delete";
 	   delSelect(selects,url,serverTable)
	});
	   ///////列表中的全选
    $("#checkAll").click(function(){
    	var check = $(this).prop("checked");
        $(".checkchild").prop("checked", check);
	});
	   ///////列表中的全选
    $("#checkAllCustomer").click(function(){
    	var check = $(this).prop("checked");
        $(".checkCustomer").prop("checked", check);
	});
 });